// pages/address/address-list.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getMemberAddress, delMemberAddr, setDefaultAddr } from '../../static/api/api.js'
//获取应用实例
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressList: [],
        pageStatus: '',
        isLoading: false,
        order_id: '',
        curAddressId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.path) {
            this.setData({
                pageStatus: 'edit',
                order_id: options.order_id || '',
                curAddressId: options.address_id || ''
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getAddressList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    getStaffList() {
        let that = this;
        
    },
    //获取地址列表
    getAddressList() {
        let that = this
        wx.showLoading({
            title: '数据加载中...',
        })
        getMemberAddress().then(response => {
            console.log(response)
            wx.hideLoading()
            that.setData({
                addressList: response.data,
                isLoading: false
            })
        }).catch(response => {
            wx.hideLoading()
        })
        
    },
    //设置默认地址
    setDefaultAddr(e) {
        let dataSet = e.currentTarget.dataset;
        let that = this
        //如果有带链接回来
        wx.showLoading({
            title: '数据加载中...',
        })
        let params={
            member_address_id: dataSet.id
        }
        setDefaultAddr(params).then(response => {
            wx.hideLoading();
            that.data.addressList.forEach((el, index) => {
                let key = 'addressList[' + index + '].is_default'
                that.setData({
                    [key]: 0
                })
            })
            let key = 'addressList[' + dataSet.index + '].is_default'
            that.setData({
                [key]: 1
            })
            if (that.data.pageStatus == "edit") {
                let pages = getCurrentPages();
                let tarPage = pages[pages.length - 2];
                wx.navigateBack()
                tarPage.chooseAddrCb(that.data.addressList[dataSet.index])
                
                return;
            }

        }).catch(response => {
            wx.hideLoading()
        })
    },
    //设置订单默认地址
    setOrderDefaultAddr(e) {
        let dataSet = e.currentTarget.dataset;
        let that = this
        if (this.data.pageStatus != 'edit') {
            return;
        }
        //如果有带链接回来
        wx.showLoading({
            title: '数据加载中...',
        })
        let params = {
            order_id: that.data.order_id,
            address_id: dataSet.id
        }
        setDefaultAddr(params).then(response => {
            wx.hideLoading();
            that.data.addressList.forEach((el, index) => {
                let key = 'addressList[' + index + '].is_default'
                that.setData({
                    [key]: 0
                })
            })
            let key = 'addressList[' + dataSet.index + '].is_default'
            that.setData({
                [key]: 1
            })
            if (that.data.pageStatus == "edit") {
                let pages = getCurrentPages();
                let tarPage = pages[pages.length - 2];
                tarPage.chooseAddrCb(that.data.addressList[dataSet.index])
                wx.navigateBack()
                return;
            }

        }).catch(response => {
            wx.hideLoading()
        })
    },
    comfirmDelAddr(e){
        let that = this;
        wx.showModal({
            title: '温馨提示',
            content: '您确定要删除当前地址吗？',
            success: function (res) {
                if (res.confirm) {
                    that.delAddrItem(e)

                } else if (res.cancel) {

                }

            }
        })
    },
    //删除地址
    delAddrItem(e) {
        let dataSet = e.currentTarget.dataset;
        let that = this
        wx.showLoading({
            title: '数据加载中...',
        })
        let params = {
            member_address_id: dataSet.id
        };
        delMemberAddr(params).then(response => {
            console.log(response)
            wx.hideLoading()
            this.data.addressList.splice(dataSet.index, 1)
            this.setData({
                addressList: that.data.addressList
            })
        }).catch(response => {
            wx.hideLoading()
        })
        
    },
    goEdit(e) {
        let dataSet = e.currentTarget.dataset;
        wx.navigateTo({
            url: dataSet.url + '?address_id=' + dataSet.id,
        })
    },
    goUrl(e) {
        let dataSet = e.currentTarget.dataset;
        wx.navigateTo({
            url: dataSet.url,
        })
    }
})